/**
 * 
 */
package com.disys.mb;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import com.disys.facade.LdapUsersFacade;
import com.disys.model.LdapUsers;

/**
 * @author Ubaid Raja
 *
 */
@SessionScoped
@ManagedBean(name="ldapUsersMB")
public class LdapUsersMB implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5488155848887843394L;
	public static final String INJECTION_NAME = "#{ldapUsersMB}";

	private LdapUsers user;
	
	private List<LdapUsers> users;

	private LdapUsersFacade usersFacade;

	public boolean isAdmin() {
		return user.isAdmin();
	}

	public boolean isDefaultUser() {
		return user.isUser();
	}

	public String logOut() {
		getRequest().getSession().invalidate();
		return "/pages/login.xhtml";
	}

	private HttpServletRequest getRequest() {
		return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	}

	public LdapUsers getUser() {
		return user;
	}

	public void setUser(LdapUsers user) {
		this.user = user;
	}


	public void setUsers(List<LdapUsers> users) {
		this.users = users;
	}

	public LdapUsersFacade getLdapUsersFacade() {
		if (usersFacade == null) {
			usersFacade = new LdapUsersFacade();
		}

		return usersFacade;
	}

	public void setUsersFacade(LdapUsersFacade usersFacade) {
		this.usersFacade = usersFacade;
	}
	
	
	public List<LdapUsers> getUsers() {
		if(users == null){
			users = new ArrayList<LdapUsers>();
		}
		users = getLdapUsersFacade().findAllLdapUsers();

		return users;
	}
}
